package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo12 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名: ");
        String username = scan.nextLine();
        if(username.isEmpty()){
            System.out.println("用户名不能为空!");
            return;
        }
        System.out.println("请输入密码: ");
        String password = scan.nextLine();
        System.out.println("请输入昵称");
        String nick = scan.nextLine();

        try (Connection conn = DBUtils.getConn();) {
            String sql = "select password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                System.out.println("用户名已存在!");
                return;
            }

            String s = "insert into user(username,password,nick) values(?,?,?)";
            ps = conn.prepareStatement(s);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,nick);
            ps.executeUpdate();
            System.out.println("恭喜您,注册成功!");

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
